package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.Cart;
import com.pro.springbootshop.entity.Collect;
import com.pro.springbootshop.entityExtend.CollectReturn;
import com.pro.springbootshop.mapper.CartMapper;
import com.pro.springbootshop.mapper.CollectMapper;
import com.pro.springbootshop.mapper.WaresMapper;
import com.pro.springbootshop.service.CollectService;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    TokenUtil tokenUtil;
    public CollectMapper collectMapper;
    @Autowired
    public void setUserMapper (CollectMapper collectMapper) {
        this.collectMapper = collectMapper;
    }
    public WaresMapper waresMapper;
    @Autowired
    public void setUserMapper (WaresMapper waresMapper) {
        this.waresMapper = waresMapper;
    }
    public CartMapper cartMapper;
    @Autowired
    public void setUserMapper (CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }
    @Override
    public String addCollect(Integer waresId, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Collect collect=new Collect();
        collect.setWaresId(waresId);
        collect.setBuyerId(buyerId);
        Collect newCollect=collectMapper.getCollect(buyerId,waresId);
        if(newCollect==null){
            int i=collectMapper.insert(collect);
            if(i>0){
                return "插入成功";
            }else{
                return "插入失败";
            }
        }else {
            return "商品已收藏";
        }
    }

    @Override
    public CollectReturn getOrderList(Integer page, Integer limit, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        int pageNo=(page-1)*limit;
        int total=collectMapper.getCollectListCount(buyerId).size();
        List<Collect> collectList= collectMapper.getCollectList(buyerId,pageNo,limit);
        for(Collect c:collectList){
            c.getWares().setPictureList(c.getWares().getWaresPicture().split(";"));
        }
        return new CollectReturn(collectList,total);
    }

    @Override
    public String deleteCollect(Integer collectId) {
        int i=collectMapper.deleteById(collectId);
        if(i>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public String insertOrder(String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        List<Cart> cartList=cartMapper.selectCartShop(buyerId);
        Collect collect=new Collect();
        int i=0;
        for(Cart cart:cartList){
            if(cart.getCartState()==1){
                collect.setWaresId(cart.getWaresId());
                collect.setBuyerId(buyerId);
                Collect newCollect=collectMapper.getCollect(buyerId,cart.getWaresId());
                if(newCollect==null){
                    i=collectMapper.insert(collect);
                }
            }
        }
        if(i>0){
            return "插入成功";
        }else {
            return "插入失败";
        }
    }
}
